package com.codes.patch.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.codes.patch.utils.TextUtils;

public class FileEntry {
	String mFilePath = null;
	List<String> mLineList = new ArrayList<String>();
	
	public FileEntry(String filePath) {
		mFilePath = filePath;
	}
	
	public void readFile() {
		if (TextUtils.isEmpty(mFilePath)) {
			return ;
		}
		
		mLineList.clear();
		
		File file = new File(mFilePath);
		if (!file.exists()) {
			return ;
		}
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String buf = null;
			while ((buf = br.readLine()) != null) {
				mLineList.add(buf + "\n");
			}
			br.close();
		} catch (IOException e) {
		}
	}
	
	public void writeFile() {
		if (TextUtils.isEmpty(mFilePath)) {
			return ;
		}
		if (mLineList == null || mLineList.isEmpty()) {
			return ;
		}
		
		File file = new File(mFilePath);
		try {
			
			if (!file.exists()) {
				file.createNewFile();
			}
			
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			for (String lineString : mLineList) {
				bw.write(lineString);
			}
			bw.flush();
			bw.close();
		} catch (IOException e) {
		}
	}
	
	public List<String> getLineList() {
		return mLineList;
	}
	
	public void setLineList(List<String> lineList) {
		mLineList = lineList;
	}
}
